package com.xujl.mvcbind.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xujl.fastlib.base.BaseActivity;
import com.xujl.fastlib.base.BaseRvAdapter;
import com.xujl.fastlib.utils.ResUtils;
import com.xujl.fastlib.utils.RouterHelper;
import com.xujl.fastlib.utils.StringUtils;
import com.xujl.mvcbind.R;
import com.xujl.mvcbind.bean.MainBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * @author x
 * @Description
 */
public class MainAdapter extends BaseRvAdapter<MainBean> {
    private BaseActivity mActivity;
    private String keyword;

    public MainAdapter (@Nullable List<MainBean> data, BaseActivity activity) {
        super(R.layout.item_main_title, data);
        mActivity = activity;
    }

    @Override
    protected void convert (BaseViewHolder helper, MainBean item) {
        helper.setText(R.id.tv, item.getName());
        TextView titleTv = helper.getView(R.id.tv);
        if (!StringUtils.isEmpty(keyword) && item.getName().contains(keyword)) {
            titleTv.setBackgroundColor(ResUtils.getColor(R.color.text_red));
        } else {
            titleTv.setBackgroundColor(ResUtils.getColor(R.color.text_green));

        }
        RecyclerView recyclerView = helper.getView(R.id.rv_child);
        recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
        BaseRvAdapter<MainBean> adapter = new BaseRvAdapter<MainBean>(R.layout.item_main_child, item.getChildList()) {
            @Override
            protected void convert (BaseViewHolder helper2, MainBean bean) {
                helper2.setText(R.id.btn, bean.getName());
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick (@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<MainBean> list = (List<MainBean>) adapter.getData();
                MainBean mainBean = list.get(position);
                if (mainBean.getActivityClass() != null) {
                    mActivity.startActivity(new Intent(mActivity, mainBean.getActivityClass()));
                    return;
                }
                if (mainBean.getFragmentClass() != null) {

                    try {
                        mActivity.start((ISupportFragment) mainBean.getFragmentClass().newInstance());
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    }
                    return;
                }
                if (!StringUtils.isEmpty(mainBean.getActivityApi())) {
                    RouterHelper.startActivity(view.getContext(), mainBean.getActivityApi());
                    return;
                }
                try {
                    mActivity.start((ISupportFragment) RouterHelper.startFragment(mainBean.getFragmentApi()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public void setKeyword (String keyword) {
        this.keyword = keyword;
    }
}
